import Vue from 'vue'
import App from './App'
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
Vue.config.productionTip = false
import md5 from 'common/md5.js'
import {
	http
} from './common/config.js'
Vue.prototype.$http = http
Vue.prototype.$httpimg = 'https://taoxichuan-bucket.oss-cn-shenzhen.aliyuncs.com'
Vue.prototype.$webImgUpload = http + '/app/communal/uploadImageVideo';
Vue.prototype.$webImgUpload2 = http + '/api/common/upload';
let pageTo = (url) => {
	uni.navigateTo({
		url:url,
	})
}

Vue.prototype.$pageTo = pageTo;
Vue.prototype.$get = (url, params) => {
	let data = params ? params : {}
	return new Promise((resolve, reject) => {
		uni.request({
			url: http + '/app/' + url,
			method: "GET",
			data:data,
			success: res => {
				if (res.data.code == 0) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					resolve(res.data)
				}
			},
			fail: e => {
				console.log(url + ":接口错误")
			}
		})
	})
}
Vue.prototype.$post = (url, params) => {
	let data = params ? params : {}
	let str = ''
	let secret = '&secret=ZQgV8HausDOD40E8u38PcYxRTrITIASs'
	if(JSON.stringify(data) == "{}"){
		
	}else{
		for(let key  in data){
					if(str==''){
						str = key +'='+data[key]
					}else{
						str = str+'&' + key +'='+data[key]
					}
		 }
		 str = str+secret
		 params.sign = md5.hexMD5(str)
	}
	 
	return new Promise((resolve, reject) => {
		uni.request({
			url: http + '/app/' + url,
			data: data,
			method: "POST",
			success: res => {
				if (res.data.code == 0) {
					resolve(res.data)
				} else if(res.data.code == -3){
					resolve(res.data)
				}else if(res.data.code == -4){
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					
					resolve(res.data)
				}
			},
			fail: e => {
				console.log(url + ":接口错误")
			}
		})
	}).catch(err => {
		console.log(err)
	})
}
Vue.prototype.$upload = function(type = 0) {
	return new Promise((resolve, reject) => {
		try {
			uni.chooseImage({
				// sourceType: ['original', 'compressed'],
				count: 1,
				success: (chooseImageRes) => {
					const tempFilePaths = chooseImageRes.tempFilePaths;
					uni.showLoading({
						title: '上传中'
					})
					let data = {type:"feedback"};
					let url = type ? '/app/communal/uploadImageVideo' : '/app/communal/uploadImageVideo'
					uni.uploadFile({
						url: http + url, //仅为示例，非真实的接口地址
						filePath: tempFilePaths[0],
						header: {
							"Content-Type": "image/jpeg"
						},
						formData: data,
						name: 'file',
						success: (uploadFileRes) => {
							uni.hideLoading()
							if (uploadFileRes.statusCode == 200) {
								// console.log(JSON.parse(uploadFileRes.data).path)
								resolve(JSON.parse(uploadFileRes.data))
							} else {
								uni.showToast({
									title: '上传失败',
									icon: "none"
								})
							}
						}
					});
				}
			});
		} catch (e) {
			//TODO handle the exception
			console.log('上传图片接口报错，请检查：' + e)
		}

	})
}
Vue.prototype.$uploads = () =>  {
	return new Promise((resolve, reject) => {
		try {
			uni.chooseImage({
				count: 4, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], //从相册选择
				success: (res) => {
					let tempFilePath = res.tempFilePaths;
					let imgs = []
					let data = {type:"feedback"};
					for (let i = 0, len = tempFilePath.length; i < len; i++) {
						uni.uploadFile({
							url: http + '/app/communal/uploadImageVideo',
							filePath: tempFilePath[i],
							name: 'file',
							formData:data,
							header: {
								
							},
							success: (res1) => {
								let data = JSON.parse(res1.data)
								console.log(res1,data)
								if (res1.statusCode == 200 && data.code == 0) {
									let res = data.data
									imgs.push(res.path)
									if (imgs.length == tempFilePath
										.length) {
										resolve(imgs)
									}
								} else {
									uni.showToast({
										title: data.msg,
										icon: "none",
										duration: 1500
									});
								}
							}
						});
					}
				}
			});
		} catch (e) {
			//TODO handle the exception
			console.log('上传图片接口报错，请检查：' + e)
		}

	})
}
Vue.prototype.$formatDate = function(date) {
	var date = new Date(date);
	var YY = date.getFullYear() + '-';
	var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
	return YY + MM + DD;
}
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()
